package com.leyou.item.web;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService groupService;

    @Autowired
    private SpecParamService paramService;

    //根据分类id查询规格组集合
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategoryId(@RequestParam("id") Long categoryId){
        List<SpecGroupDTO> specGroupDTOList = groupService.queryGroupByCategoryId(categoryId);
        return ResponseEntity.ok(specGroupDTOList);
    }

    //根据复杂条件查询规格参数集合
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(@RequestParam(value = "categoryId",required = false) Long categoryId,
                                                              @RequestParam(value = "groupId",required = false) Long groupId,
                                                              @RequestParam(value = "searching",required = false) Boolean searching){
        List<SpecParamDTO> specParamDTOList = paramService.querySpecParams(categoryId,groupId,searching);
        return ResponseEntity.ok(specParamDTOList);
    }

    //根据分类id查询规格组及规格参数
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long categoryId){
        List<SpecGroupDTO> specGroupDTOList = groupService.querySpecList(categoryId);
        return ResponseEntity.ok(specGroupDTOList);
    }



    //新增规格组
    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDTO){
        groupService.saveGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //修改规格组
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO specGroupDTO){
        groupService.updateGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //新增规格参数
    @PostMapping("/param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO specParamDTO){
        paramService.saveParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //修改规格参数
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO specParamDTO){
        paramService.updateParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
